--
-- Created by IntelliJ IDEA.
-- User: Administrator
-- Date: 2019/2/24 0024
-- Time: 下午 4:23
-- To change this template use File | Settings | File Templates.
--阵型

local INTERVAL_WIDTH = 5   -- 间隔宽度
local Formation = {}

local function formatData(data)
    -- 规整为列表结构
    local stance = {}
    for y = 1, #data.formation do
        for x = 1, #data.formation[y] do
            if data.formation[y][x] > 0 then
                local existTbl = stance[data.formation[y][x]]
                if existTbl ~= nil then
                    assert(nil, "formation [" .. data.id .. "]" .. " error. current position = " .. y .. ":" .. x, "exist position = " .. existTbl.y .. ":" .. existTbl.x, "index = " .. data.formation[y][x])
                end
                stance[data.formation[y][x]] = {
                    x = x,
                    y = y
                }
            end
        end
    end
    data.stance = stance
    Formation[data.id] = data
end

local quene_formationData = {
    id = "quene1",
    moveAdd = 30,                   -- 移动加成
    farAttackAdd = 0,               -- 远程加成
    nearAttackAdd = 30,             -- 近战加成
    defenceAdd = 5,                 -- 防御加成
    moraleAdd = -5,                 -- 士气加成
    trip = {},                      -- 触发
    formation = {                   -- 阵型结构
        {0,0,0,         29, 39, 59, 79, 91,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         27, 37, 57, 77, 93,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         25, 35, 55, 75, 95,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         23, 33, 53, 73, 97,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         21, 31, 51, 71, 99,        0,0, 0,0,0,0,0 ,0,0,0,0,0},

        {0,0,0,         9 , 19, 49, 69, 81,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         7 , 17, 47, 67, 83,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         5 , 15, 45, 65, 85,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         3 , 13, 43, 63, 87,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         1 , 11, 41, 61, 89,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
         
        {0,0,0,         2 , 12, 42, 62, 88,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         4 , 14, 44, 64, 86,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         6 , 16, 46, 66, 84,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         8 , 18, 48, 68, 82,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         10, 20, 50, 70, 90,        0,0, 0,0,0,0,0 ,0,0,0,0,0},

        {0,0,0,         22, 32, 52, 72, 98,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         24, 34, 54, 74, 96,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         26, 36, 56, 76, 94,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         28, 38, 58, 78, 92,        0,0, 0,0,0,0,0 ,0,0,0,0,0},
        {0,0,0,         30, 40, 60, 80, 100,       0,0, 0,0,0,0,0 ,0,0,0,0,0},
    }
}

formatData(quene_formationData)


function Formation:getFormation(id) 
    return Formation[id]
end

-- firstPosition: {x=,y=} 一号位置在哪里
-- units: 单元集合，从第一个开始排列起
-- formation: 已经初始化的阵型数据
-- faceAngelArc: 弧度
function Formation:standUnits(firstPosition, units, formation, faceAngelArc)
    if units == nil or #units <= 0 then
        return    
    end
    
    if faceAngelArc == nil then
        faceAngelArc = 0
    end
    logW("standUnits faceAngelArc : " .. faceAngelArc)
    formation = formation.stance
    local fx, fy = formation[1].x, formation[1].y

    for i = 1, #units do
        local unit = units[i]
        local pos = {}
        local fm = formation[i]
        if faceAngelArc == 0 then
            pos.x = firstPosition.x + (fm.x - fx) * INTERVAL_WIDTH
            pos.y = firstPosition.y + (fm.y - fy) * INTERVAL_WIDTH
        else
            local r = math.sqrt((fm.x - fx) * (fm.x - fx) + (fm.y - fy) * (fm.y - fy)) * INTERVAL_WIDTH
            local theta = math.atan2(fm.y - fy, fm.x - fx) + faceAngelArc
            pos.x = firstPosition.x + math.cos(theta) * r
            pos.y = firstPosition.y + math.sin(theta) * r
        end
        unit:moveOrder(pos)
    end
end

return Formation